import { loginHandle } from '@/utils/mpWx.js'
export default {
	data() {
		return {
			isClick: false
		}
	},
	methods: {
		// 取消订单
		onCancelOrder(type, id) {
			if (this.isClick) return
			this.isClick = true
			uni.showModal({
				title: '提示',
				content: '确认取消当前赛事报名吗？',
				success: (res) => {
					if (res.confirm) {
						/* prettier-ignore */
						uni.showLoading({ title: '提交中~', mask: true })
						this.$https(true, 'get', {
							url: '/member/order/cancel/' + id,
							data: {}
						}, (res) => {
							uni.$u.toast('报名已取消')
							if (type === 'list') {
								this.onRefreshData()
							} else {
								this.getDetailInfo()
							}
							this.isClick = false
						}, (error) => {
							this.isClick = false
						})
						/* prettier-ignore */
					} else if (res.cancel) {
						this.isClick = false
						console.log('用户点击取消');
					}
				}
			})
		},
		// 继续付款
		onGoPayment(type, raceId) {
			if (this.isClick) return
			this.isClick = true
			uni.showModal({
				title: '提示',
				content: '是否确认支付当前赛事报名？',
				success: async (res) => {
					if (res.confirm) {
						const openid = await loginHandle()
						/* prettier-ignore */
						uni.showLoading({ title: '加载中', mask: true })
						this.$https(true, 'post', {
							url: '/order/order/pay',
							data: {
								openId: openid,
								raceId
							}
						}, (res) => {
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.timeStamp, //当前的时间
								nonceStr: res.data.nonceStr, //随机字符串
								package: 'prepay_id=' + res.data.prepayId,
								signType: 'MD5', //签名算法，暂支持 MD5。
								paySign: res.data.paySign, //签名
								success: (pay) => {
									uni.$u.toast('报名成功！')
									if (type === 'list') {
										this.onRefreshData()
									} else {
										this.getDetailInfo()
									}
									this.isClick = false
								},
								fail: (error) => {
									console.log('失败', error);
									this.isClick = false
									uni.$u.toast('支付失败，请重试！')
								}
							})
						}, (err) => {
							this.isClick = false
						})
						/* prettier-ignore */
					} else if (res.cancel) {
						this.isClick = false
						console.log('用户点击取消');
					}
				}
			})
		},
		// 申请退款
		onReturnConfirm(type, id) {
			if (this.isClick) return
			this.isClick = true
			uni.showModal({
				title: '提示',
				content: '退款后报名资格会取消，确认要申请退款吗？',
				success: (res) => {
					if (res.confirm) {
						/* prettier-ignore */
						uni.showLoading({ title: '提交中', mask: true })
						this.$https(true, 'get', {
							url: '/member/order/refund/' + id,
							data: {}
						}, (res) => {
							uni.$u.toast('提交成功！')
							if (type === 'list') {
								this.onRefreshData()
							} else {
								this.getDetailInfo()
							}
							this.isClick = false
						}, (error) => {
							this.isClick = false
						})
						/* prettier-ignore */
					} else if (res.cancel) {
						this.isClick = false
						console.log('用户点击取消');
					}
				}
			})
		},
	}
}